package uit.nfcmenu.waiter.controllers;

import java.util.ArrayList;

import uit.nfcmenu.waiter.activities.OrderListActivity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import uit.nfcmenu.models.OrderChef;
import uit.nfcmenu.models.OrderModel;
import uit.nfcmenu.utils.CommConstant;
import uit.nfcmenu.utils.CommConstant.OrderStates;
import uit.nfcmenu.waiter.R;

/*********************** Databind data for ListItemModel *************************************************************/
// The adapter class!
public class ListOrderStatusModelsAdapter extends BaseAdapter {

	// Inflater for inflated view
	private LayoutInflater mInflater;
	private ArrayList<OrderChef> mArrItemModelsList;
	private Context ctx;
	DatabaseManagerChef mDBManagerChef;
	OrderListActivity parent;

	// constructor
	public ListOrderStatusModelsAdapter(Context context,
			ArrayList<OrderChef> arrListView, OrderListActivity parent) {
		mInflater = LayoutInflater.from(context);
		// convert(arrListView);
		// mArrItemModelsList = arrListView;
		ctx = context;
		this.parent = parent;
		mDBManagerChef = new DatabaseManagerChef(parent);
		mArrItemModelsList = arrListView;
	}

	// get total amount of list items
	public int getCount() {
		return mArrItemModelsList.size();
	}

	// get the list item given its position
	public Object getItem(int position) {
		return mArrItemModelsList.get(position);
	}

	// get the list item's ItemModelName given its position
	public long getItemId(int position) {
		return mArrItemModelsList.get(position).getOrderID();
	}

	private void makeDeny(final int pos) {
		View layout = mInflater.inflate(R.layout.popup_comment, null);
		final EditText tvComment = (EditText) layout
				.findViewById(R.id.txtComment);

		tvComment.setText(mArrItemModelsList.get(pos).getComment());
		AlertDialog dialog = new AlertDialog.Builder(ctx)
				.setView(layout)
				.setTitle("Commnent")
				.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						String comment = tvComment.getText().toString();
						// Work with Database to save Comment

						mArrItemModelsList.get(pos).setComment(comment);
						mDBManagerChef.editStatus(mArrItemModelsList.get(pos)
								.getOrderID(), "DENY",
								mArrItemModelsList.get(pos).getComment());
						

						dialog.dismiss();
					}
				})
				.setNegativeButton("Cancel",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
							}
						}).create();
		dialog.show();
	}

	private void showComment(final int pos) {

		AlertDialog dialog = new AlertDialog.Builder(ctx).setTitle("Commnent")
				.setMessage(mArrItemModelsList.get(pos).getComment())
				.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
					
						// Work with Database to save Comment

						dialog.dismiss();
					}
				}).create();

		dialog.show();
	}

	// Project view of the list
	public View getView(final int position, View convertView, ViewGroup parent) {
		// declare view holder to hold custom list contents
		ViewHolder holder;

		// if the holder's not setup, initiate its content widgets
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.list_inflater, null);
			holder = new ViewHolder();
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.tvItem = (TextView) convertView.findViewById(R.id.tvItem);
		holder.tvTable = (TextView) convertView.findViewById(R.id.tvTable);
		holder.tvQuantity = (TextView) convertView.findViewById(R.id.tvQuantity);
		holder.tvTime = (TextView) convertView.findViewById(R.id.tvTime);
		holder.btnStatus = (Button) convertView.findViewById(R.id.btnStatus);
		holder.btnDeny = (Button) convertView.findViewById(R.id.btnDeny);
		holder.tvComment = (TextView) convertView.findViewById(R.id.txtComment);
		convertView.setTag(holder);
		holder.btnDeny.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Button btn = ((Button) (v));
				String btnDenyStr = btn.getText().toString().toUpperCase();
				if (btnDenyStr.equals("REMOVE")) {
					mArrItemModelsList.remove(position);
					notifyDataSetChanged();
				} else if (OrderStates.valueOf(btnDenyStr) == OrderStates.DONE) {
					// update status to database
					mArrItemModelsList.get(position).setStatus(OrderStates.COOKED.name());
					mDBManagerChef.editStatus(mArrItemModelsList.get(position)
							.getOrderID(), OrderStates.COOKED.name(),
							mArrItemModelsList.get(position).getComment());
					notifyDataSetChanged();
				} else if (OrderStates.valueOf(btnDenyStr) == OrderStates.DENY) {
					makeDeny(position);
				} else {
					mArrItemModelsList.remove(position);
					notifyDataSetChanged();
				}
			}
		});

		holder.btnStatus.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub

				Button btn = ((Button) (v));
				String name = btn.getText().toString();
				if ((OrderStates.valueOf(name) == OrderStates.COOKED)
						|| (OrderStates.valueOf(name) == OrderStates.DENY)) {
					return;
				}
				// TODO change to StateOrder in enum
				String nextStatus = mArrItemModelsList.get(position) 
						.getNextstatus();
				if (!nextStatus.equals("")) {
					mDBManagerChef.editStatus(mArrItemModelsList.get(position)
							.getOrderID(), nextStatus,
							mArrItemModelsList.get(position).getComment());
					btn.setText(nextStatus);

				}

				Log.i("nndai", "" + position);
				notifyDataSetChanged();
			}
		});

		OrderChef orderModel = null;

		// get the selected schedule
		orderModel = mArrItemModelsList.get(position);
		// assign contents
		// will change
		String itemName = CommConstant.item.get(orderModel.getItemID());
		holder.tvItem.setText("" + itemName);
		holder.tvTable.setText("" + orderModel.getTableNo());
		holder.tvQuantity.setText("" + orderModel.getQuantity());
		holder.tvTime.setText("" + orderModel.getTimeRemain());
		holder.btnStatus.setText("" + orderModel.getStatus());
		getDenyButtonText(orderModel.getStatus(), holder.btnDeny);
		holder.tvComment.setText(orderModel.getComment());
		if (orderModel.getTime() < 0) {
			convertView.setBackgroundColor(Color.RED);
		} 
		else {
			convertView.setBackgroundColor(ctx.getResources().getColor(
					R.color.background));
		}
		convertView.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showComment(position);
			}
		});
		return convertView;
	}

	// Getter, Setter
	public ArrayList<OrderChef> getItemModels() {
		return mArrItemModelsList;
	}

	public void setItemModels(ArrayList<OrderChef> arrItemModels) {

		notifyDataSetChanged();
		notifyDataSetInvalidated();
	}

	// view holder model
	class ViewHolder {
		TextView tvItem;
		TextView tvTable;
		TextView tvQuantity;
		TextView tvTime;
		Button btnStatus;
		Button btnDeny;
		TextView tvComment;
	}

	private void getDenyButtonText(String status, Button btn) {
		if (status != null
				&& (status.equalsIgnoreCase("new")
						|| status.equalsIgnoreCase("waiting") || status
							.equalsIgnoreCase("deny"))) {
			btn.setText(OrderStates.DENY.name());
			btn.setBackgroundDrawable((ctx.getResources()
					.getDrawable(R.drawable.bt_deny)));
		} else if (status.equalsIgnoreCase("cooked")) {
			btn.setText("REMOVE");
			btn.setBackgroundDrawable((ctx.getResources()
					.getDrawable(R.drawable.bt_deny)));
		} else {
			btn.setText(OrderStates.DONE.name());
			btn.setBackgroundDrawable((ctx.getResources()
					.getDrawable(R.drawable.but_status_cooking)));
		}
	}

	private void convert(ArrayList<OrderModel> input) {
		mArrItemModelsList = new ArrayList<OrderChef>();
		for (int i = 0; i < input.size(); i++) {
			OrderChef model = new OrderChef(input.get(i));
			mArrItemModelsList.add(model);
		}
	}
}
